/**
 * Created by 3fuyu on 16/8/22.
 */

define(['./PageView', "jquery", '../libs/AjaxUtil', '../libs/scrollBottom', '../libs/CommonTools'], function (PageView, $, AjaxUtil, ScrollBottom, CommonTools) {
    "use strict";

    require('../../less/SelectDeptList.less');
    return PageView.extend({
        events: {
            'click .dept-name': 'goNextDept',
            'click .select-circle': 'selectDept',
            'click .left': 'goBack',
            'click .dept-submit': 'onSubmit',
        },
        initialize: function () {
            // 辅助回到上级部门标志
            this.deptId = '';
        },
        render: function () {
            PageView.prototype.render.apply(this, arguments);
            this.$wrap = $('<div class="main"><div class="content"></div></div><div class="select-dept-bottom"><span><div class="dept-selected">请选择部门</div></span><span><div class="dept-submit">确定</div></span></div>');
            this.$el.append(this.$wrap);
            return this;
        },
        onResume: function () {
            this.onRender();
        },
        onRender: function () {

            // 检测是否到顶级
            if (window.location.hash.indexOf('?') > -1) {
                this.deptId = window.location.hash.split('?')[1].split('=')[1];
            } else {
                this.deptId = '';
            }

            var temp = require('../templates/SelectDeptList.html');
            this.$el.find('.content').html(temp);
            this.getList();
        },
        getList: function () {
            var _this = this,
                params = {},
                tpl = '';

            params.parentId = _this.deptId;

            AjaxUtil.ajaxGet({
                path: '/client/dept/get', params: params, success: function (data) {
                    if (data.length != 0) {
                        data.forEach(function (it) {
                            tpl += '<li class="dept-list' + (it.hasNext ? 'has-next icon-right' : '') + '" deptId=' + it.deptId + ' ><div class="select-circle icon-circl"></div><div class="dept-name"><span class="dept-name-content">' + it.deptName + '</span>' + (it.ownerDept === 0 ? '<span class="my-dept">我的部门</span>' : '') + '</div></li>';
                        });

                        _this.$el.find('.dept-lists').append(tpl);
                    }
                }, dataEmpty: function () {
                }
            });
        },
        goNextDept: function (e) {
            var $target = $(e.currentTarget);

            this.deptId = $target.parent().attr('deptId');

            if ($target.parent().hasClass('icon-right')) {
                this.deleteSelectDept();
                Backbone.history.navigate('#SelectDeptList?deptId=' + this.deptId, {trigger: true});
            } else {
                $target.prev().click();
            }
        },
        selectDept: function (e) {
            var $target = $(e.currentTarget),
                deptName = $target.next().find('.dept-name-content').html(),
                deptId = $target.parent().attr('deptId');

            // 删除同辈元素中选中
            if ($target.parent().siblings() && $target.parent().siblings().find('.select-circle')
                && $target.parent().siblings().find('.select-circle').length > 1) {
                _.each($target.parent().siblings().find('.select-circle'), function (value, key) {
                    $(value).removeClass('icon-correct');
                });
            }

            if ($target.hasClass('icon-correct')) {
                $target.removeClass('icon-correct');

                this.$el.find('.dept-selected').html('请选择部门');
            } else {
                $target.addClass('icon-correct');
                this.$el.find('.dept-selected').html(deptName);
                this.$el.find('.dept-selected').attr('deptId', deptId);
            }
        },
        // 删除之前选中
        deleteSelectDept: function () {
            if (this.$el.find('.select-circle')) {
                _.each(this.$el.find('.select-circle'), function (value, key) {
                    $(value).removeClass('icon-correct');
                });

                this.$el.find('.dept-selected').html('请选择部门');
            }
        },
        goBack: function () {
            this.deleteSelectDept();
        },
        onSubmit: function () {
            var $target = this.$el.find('.dept-selected'),
                deptName = $target.html(),
                deptId = $target.attr('deptId');
            if (deptName && deptName !== '请选择部门') {
                var deptObj = {
                    deptId: deptId,
                    deptName: deptName
                };
                sessionStorage.setItem('SELECTED_DEPT', JSON.stringify(deptObj));

                Backbone.history.navigate('#AllOKRs', {trigger: true});
            } else {
                CommonTools.Toast('请选择部门');
            }
        }
    });
});